﻿using Sidvall.UI.Dialogs;
using System.Threading.Tasks;

namespace Sidvall.UI.Wpf.Dialogs
{
    public class FileDialog : IFileDialog
    {
        #region Public Members

        #region OpenFilesAsync

        public Task OpenFilesAsync(OpenFileDialogContext context)
        {
            if (context == null)
                throw new System.ArgumentNullException("context");

            this.Context = context;
            var dialog = new Microsoft.Win32.OpenFileDialog();
            InitializeFileDialog(dialog, this.Context);
            dialog.Multiselect = this.Context.AllowMultipleSelect;
            if (dialog.ShowDialog() == true)
            {
                foreach (var fileName in dialog.FileNames)
                    this.Context.FileNames.Add(fileName);
                this.Context.FileName = dialog.FileName;
            }
            else
            {
                this.Context.FileName = null;
            }
            return Task.FromResult(true);
        }

        #endregion
        #region SaveFileAsync

        public Task SaveFileAsync(SaveFileDialogContext context)
        {
            if (context == null)
                throw new System.ArgumentNullException("context");

            var dialog = new Microsoft.Win32.SaveFileDialog();
            InitializeFileDialog(dialog, context);
            dialog.CreatePrompt = context.CreatePrompt;
            dialog.OverwritePrompt = context.OverwritePrompt;
            if (dialog.ShowDialog() == true)
            {
                context.FileName = dialog.FileName;
            }
            else
            {
                context.FileName = null;
            }
            return Task.FromResult(true);
        }

        #endregion

        #endregion
        #region Private Members

        private OpenFileDialogContext Context { get; set; }

        #region InitializeFileDialog

        private static void InitializeFileDialog(Microsoft.Win32.FileDialog dialog, FileDialogContext context)
        {
            dialog.FileName = context.FileName;
            dialog.Filter = context.FileTypeChoices.ToString();
            dialog.DefaultExt = context.DefaultExtension;
            dialog.FilterIndex = context.FilterIndex;
            dialog.CheckPathExists = context.CheckPathExists;
            if (context.InitialFolder != null)
            {
                switch (context.InitialFolder.Value)
                {
                    case Sidvall.IO.FolderType.InstalledLocation:
                        dialog.InitialDirectory = System.Reflection.Assembly.GetExecutingAssembly().Location;
                        break;

                    case Sidvall.IO.FolderType.LocalApplicationData:
                        dialog.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);
                        break;
                    case Sidvall.IO.FolderType.RoamingApplicationData:
                        dialog.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData);
                        break;
                    case Sidvall.IO.FolderType.TemporaryApplicationData:
                        dialog.InitialDirectory = System.IO.Path.GetTempPath();
                        break;

                    case Sidvall.IO.FolderType.Desktop:
                        dialog.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
                        break;
                    case Sidvall.IO.FolderType.MyDocuments:
                        dialog.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
                        break;
                    case Sidvall.IO.FolderType.MyMusic:
                        dialog.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyMusic);
                        break;
                    case Sidvall.IO.FolderType.MyPictures:
                        dialog.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures);
                        break;
                    case Sidvall.IO.FolderType.MyVideos:
                        dialog.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyVideos);
                        break;
                    default:
                        throw new System.ArgumentException(nameof(context));
                }
            }
        }

        #endregion

        #endregion
    }
}
